package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.goods.api.RemoteGoodsService;
import com.ruoyi.system.domain.SysRegion;
import com.ruoyi.system.mapper.SysRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BmWarehouseMapper;
import com.ruoyi.system.api.domain.BmWarehouse;
import com.ruoyi.system.service.IBmWarehouseService;

/**
 * #仓库信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-02
 */
@Service
public class BmWarehouseServiceImpl implements IBmWarehouseService 
{
    @Autowired
    private BmWarehouseMapper bmWarehouseMapper;
    @Autowired
    private SysRegionMapper sysRegionMapper;
    @Autowired
    private RemoteGoodsService remoteGoodsService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询#仓库信息
     * 
     * @param id #仓库信息主键
     * @return #仓库信息
     */
    @Override
    public BmWarehouse selectBmWarehouseById(Long id)
    {
        BmWarehouse bmWarehouse = bmWarehouseMapper.selectBmWarehouseById(id);
        List<String> city = Arrays.asList(bmWarehouse.getCity().split("-"));
        String districtName = city.get(2);
        SysRegion sysRegion = sysRegionMapper.selectSysRegionByDistrictName(districtName);
        List<String> cityCode=new ArrayList<>();
        cityCode.add(sysRegion.getProvinceCode());
        cityCode.add(sysRegion.getCityCode());
        cityCode.add(sysRegion.getDistrictCode());
        bmWarehouse.setCityCode(cityCode);
        return bmWarehouse;
    }

    /**
     * 查询#仓库信息
     *
     * @param warehouseCode #仓库编号
     * @return #仓库信息
     */
    @Override
    public BmWarehouse selectBmWarehouseByCode(String warehouseCode) {
        return bmWarehouseMapper.selectBmWarehouseByCode(warehouseCode);
    }

    /**
     * 查询#仓库信息列表
     * 
     * @param bmWarehouse #仓库信息
     * @return #仓库信息
     */
    @Override
    public List<BmWarehouse> selectBmWarehouseList(BmWarehouse bmWarehouse)
    {
        return bmWarehouseMapper.selectBmWarehouseList(bmWarehouse);
    }

    /**
     * 新增#仓库信息
     * 
     * @param bmWarehouse #仓库信息
     * @return 结果
     */
    @Override
    public int insertBmWarehouse(BmWarehouse bmWarehouse)
    {
        redisService.setCacheObject("warehouseShunxu:" + bmWarehouse.getWarehouseCode(),0);
        redisService.setCacheObject("wuliuShunxu:" + bmWarehouse.getWarehouseCode(),0);
        List list = new ArrayList();
        list.add(0);
        redisService.setCacheList("warehouseAnjian:" + bmWarehouse.getWarehouseCode(),list);
        redisService.setCacheList("wuliuAnjian:" + bmWarehouse.getWarehouseCode(),list);
        return bmWarehouseMapper.insertBmWarehouse(bmWarehouse);
    }

    /**
     * 修改#仓库信息
     * 
     * @param bmWarehouse #仓库信息
     * @return 结果
     */
    @Override
    public int updateBmWarehouse(BmWarehouse bmWarehouse)
    {
        return bmWarehouseMapper.updateBmWarehouse(bmWarehouse);
    }

    /**
     * 批量删除#仓库信息
     * 
     * @param ids 需要删除的#仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteBmWarehouseByIds(Long[] ids)
    {
        return bmWarehouseMapper.deleteBmWarehouseByIds(ids);
    }

    /**
     * 删除#仓库信息信息
     * 
     * @param id #仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteBmWarehouseById(Long id)
    {
        return bmWarehouseMapper.deleteBmWarehouseById(id);
    }

    @Override
    public Boolean mergeWarehouse(Long[] ids) {
        List<Long> warehouseIds=new ArrayList<>();
        List warehouseCodes=new ArrayList<>();
        for (Long id : ids) {
            BmWarehouse bmWarehouse = bmWarehouseMapper.selectBmWarehouseById(id);
            String warehouseCode = bmWarehouse.getWarehouseCode();
            Integer goodNum = remoteGoodsService.getGoodsNumByWarehouseCode(warehouseCode);
            if (goodNum==0){
                warehouseIds.add(bmWarehouse.getId());
                warehouseCodes.add(warehouseCode);
            }
        }
        Boolean result=null;
        if (warehouseIds.size() == ids.length-1){
            Long[] warehouseIdsArray = warehouseIds.toArray(new Long[0]);
            result=bmWarehouseMapper.deleteBmWarehouseByIds(warehouseIdsArray)>0;
        }else {
            result=false;
        }
        return result;
    }

    @Override
    public Long selectMaxId() {
        return bmWarehouseMapper.selectMaxId();
    }
}
